﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebAPIDemo.Models;

namespace WebAPIDemo.Controllers
{
    public class ContactAPIController : ApiController
    {
        List<Contact> Contacts = new List<Contact>
        {
            new Contact{Email="teo@gmail.com", Name="Nguyễn Văn Tèo", Phone="098765432"},
            new Contact{Email="phuong@gmail.com", Name="Trần Minh Phương", Phone="093286345"},
            new Contact{Email="hanh@gmail.com", Name="Phạm Thị Hạnh", Phone="0982987654"}
        };
        
        // GET api/contactapi
        public List<Contact> Get()
        {
            return Contacts;
        }

        // GET api/contactapi/5
        public Contact Get(String Id)
        {
            var contact = Contacts.Single(c => c.Email == Id);
            return contact;
        }

        // POST api/contactapi
        public List<Contact> Post(Contact contact)
        {
            Contacts.Add(contact);
            return Contacts;
        }

        // PUT api/contactapi/5
        public List<Contact> Put(String Id, Contact contact)
        {
            var oldContact = Contacts.Single(c => c.Email == Id);
            oldContact.Email = contact.Email;
            oldContact.Name = contact.Name;
            oldContact.Phone = contact.Phone;

            return Contacts;
        }

        // DELETE api/contactapi/5
        public List<Contact> Delete(String Id)
        {
            var contact = Contacts.Single(c => c.Email == Id);
            Contacts.Remove(contact);
            
            return Contacts;
        }
    }
}
